package com.yc.game.cms.controller;

import com.github.pagehelper.PageInfo;
import com.yc.game.cms.common.ServerResponse;
import com.yc.game.cms.pojo.Training;
import com.yc.game.cms.pojo.TrainingContent;
import com.yc.game.cms.service.TrainingContentService;
import com.yc.game.cms.service.TrainingService;
import com.yc.game.cms.vo.TrainingVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class TrainingController {
    @Autowired
    private TrainingContentService trainingContentService;
    @Autowired
    private TrainingService trainingService;
    /**
     * 查询所有的发布代练信息
     */
    @RequestMapping("training/getAll")
    public String getAllTraining(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                 @RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize,
                                 ModelMap modelMap) {
        PageInfo<TrainingContent> pageInfo = trainingService.getAll(pageNum, pageSize);
        modelMap.addAttribute("pageInfo", pageInfo);
        return "training";
    }

    /**
     * 修改审核状态
     */
    @PostMapping("training/editTraining")
    @ResponseBody
    public ServerResponse editTraining(TrainingContent trainingContent) {
        int result = trainingContentService.update(trainingContent);
        if (result > 0) {
            return ServerResponse.createBySuccessMessage("操作成功");
        }
        return ServerResponse.createByErrorMessage("操作失败");
    }
    /**
     * 删除
     */
    @RequestMapping("training/deleteById")
    @ResponseBody
    public ServerResponse deleteById(@RequestParam("trainingId") Integer trainingId) {
        int result = trainingContentService.deleteById(trainingId);
        if (result > 0) {
            return ServerResponse.createBySuccessMessage("操作成功");
        }
        return ServerResponse.createByErrorMessage("操作失败");
    }

    /**
     * 查询所有代练功能
     */
    @RequestMapping("training/findAll")
    public String findAll(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                          @RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize,
                          ModelMap modelMap) {
        PageInfo<TrainingVo> pageInfo = trainingContentService.findAll(pageNum, pageSize);
        modelMap.addAttribute("pageInfo", pageInfo);
        return "trainingVo";
    }

    /**
     * 更新代练交易信息
     */
    @RequestMapping("training/updateTraining")
    @ResponseBody
    public ServerResponse updateTraining(Training training) {
        int result = trainingService.update(training);
        if (result > 0) {
            return ServerResponse.createBySuccessMessage("操作成功");
        }
        return ServerResponse.createByErrorMessage("操作失败");
    }

    /**
     * 删除交易信息
     */
    @RequestMapping("trainingVo/deleteById")
    @ResponseBody
    public ServerResponse deleteByTrainingVoId(@RequestParam("trainingId") Integer trainingId) {
        int result = trainingService.delete(trainingId);
        if (result > 0) {
            return ServerResponse.createBySuccessMessage("操作成功");
        }
        return ServerResponse.createByErrorMessage("操作失败");
    }
}
